<template>
  <div>
    <div>
      <el-card style="margin: 10px">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>志愿者</el-breadcrumb-item>
          <el-breadcrumb-item>志愿者管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-row :gutter="20">
          <el-col :span="8">
            <el-input style="top: 20px" placeholder="请输入" v-model="name">
              <el-button
                slot="append"
                class="el-icon-search"
                @click="getList"
              ></el-button>
            </el-input>
          </el-col>
          <el-col :span="8">
            <el-button type="primary" class="btn" @click="fromClick"
              >添加志愿者</el-button
            >
            <el-button type="success" class="btn" @click="addSearch"
              >更多搜索</el-button
            >
          </el-col>
        </el-row>
        <el-row :gutter="12" v-show="Search" style="margin:10px -5px">
          <el-col :span="8"
            ><el-input
              style="top: 20px"
              placeholder="请输入id"
              v-model="id"
              :clearable="true"
              @clear="getList"
            >
              <el-button
                slot="append"
                class="el-icon-search"
                @click="getList"
              ></el-button> </el-input
          ></el-col>
          <el-col :span="8"
            ><el-input
              style="top: 20px"
              placeholder="请输入账号"
              v-model="username"
              :clearable="true"
              @clear="getList"
            >
              <el-button
                slot="append"
                class="el-icon-search"
                @click="getList"
              ></el-button> </el-input
          ></el-col>
        </el-row>
      </el-card>
      <el-card>
        <el-table :data="tableData">
          <el-table-column prop="id" label="ID"> </el-table-column>
          <el-table-column prop="position" label="志愿者"> </el-table-column>
          <el-table-column prop="workRequire" label="招聘要求"> </el-table-column>
          <el-table-column prop="workSalary" label="工资待遇"> </el-table-column>
          <el-table-column prop="workPlace" label="工作地点"> </el-table-column>
          <el-table-column prop="workTime" label="工作时间"> </el-table-column>
          <el-table-column prop="recruitsNum" label="招聘人数"> </el-table-column>
          <el-table-column prop="lead" label="负责人"> </el-table-column>
          <el-table-column prop="contact" label="联系方式"> </el-table-column>
          <el-table-column prop="picture" label="图片">
            <template slot-scope="scope">
              <img :src="scope.row.picture" width="100px" height="100px">
            </template>
          </el-table-column>
          <el-table-column label="操作" width="150">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="primary"
                @click="userFrom(scope.row.id)"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="removeUser(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <div class="block">
          <el-pagination
            layout="prev, pager, next"
            :total= total
            :page-size = pageSize
            @current-change="change"
          >
          </el-pagination>
        </div>
      </el-card>
      <!-- 打开添加框 -->
      <el-dialog
        title="添加信息"
        :visible.sync="dialogFormVisible"
        width="500px"
      >
        <el-form :model="formAdd">
          <el-form-item label="ID">
            <el-input v-model="formAdd.id"></el-input>
          </el-form-item>
          <el-form-item label="志愿者">
            <el-input v-model="formAdd.position"></el-input>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input v-model="formAdd.contact"></el-input>
          </el-form-item>
          <el-form-item label="负责人">
            <el-input v-model="formAdd.lead"></el-input>
          </el-form-item>
          <el-form-item label="图片">
            <el-upload
              class="upload-demo"
              multiple
              accept="image/jpeg,image/png,image/jpg"
              :limit="1"
              action="action"
              :http-request="uploadFile"
              :auto-upload="true"
              ref="upload"
            >
              <el-button size="small" type="primary">点击上传</el-button>
              <div slot="tip" class="el-upload__tip">
                只能上传jpg/png文件，且不超过500kb
              </div>
            </el-upload>
          </el-form-item>
        </el-form>
        <div class="dialog-footer" style="top: 10px">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="onFromUser">确 定</el-button>
        </div>
      </el-dialog>

      <!--点击更改-->
      <el-dialog
        title="更改信息"
        :visible.sync="dialogFormVisibleInfo"
        width="500px"
      >
        <el-form :model="fromUpdateUser">
          <el-form-item label="ID">
            <el-input v-model="fromUpdateUser.id"></el-input>
          </el-form-item>
          <el-form-item label="志愿者">
            <el-input v-model="fromUpdateUser.position"></el-input>
          </el-form-item>
          <el-form-item label="联系方式">
            <el-input v-model="fromUpdateUser.contact"></el-input>
          </el-form-item>
          <el-form-item label="负责人">
            <el-input v-model="fromUpdateUser.lead"></el-input>
          </el-form-item>
        </el-form>
        <div class="dialog-footer" style="top: 10px">
          <el-button @click="dialogFormVisibleInfo = false">取 消</el-button>
          <el-button type="primary" @click="onUpdateUser">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
export default {
  name:'zhiyuanzhe',
  data() {
    return {
      formAdd: {
        id: "32",
        position: "",
        contact: "",
        lead: "",
        picture: ''
      },
      fromUpdateUser: {
        id: "",
        position: "",
        contact: "",
        lead: "",
      },
      tableData: [],
      pageSize: 3,
      pageNum: 1,
      dialogFormVisible: false,
      dialogFormVisibleInfo: false,
      name: "",
      total:null,
      imgUrl:'',
      Search:'',
      id: "",
      username: "",
    };
  },
  methods: {
    async getList() {
      let { data: res } = await this.$axios.get("/volunteer/getlist", {
        params: {
          pageSize:this.pageSize,
          pageNum:this.pageNum,
          name: this.name,
          id: this.id,
          username: this.username,
        },
      });
      this.tableData = res.data.list;
      // console.log(res.data.list[0]);
      this.total = res.data.total
      // this.pageNum = res.data.pageNum
    },
    // 当前页发生改变时,触发该函数
    change(e) {
      this.pageNum = e;
      this.getList(this.pageSize, e);
    },
    fromClick() {
      this.dialogFormVisible = true;
      let time = this.nowTime();
      this.formAdd.addtime = time;
    },

    // 上传图片
    async uploadFile() {
      const fromData = new FormData();
      const file = this.$refs.upload.uploadFiles[0];
      const headerConfig = {
        headers: { "Content-Type": "multipart/from-data" },
      };
      if (!file) {
        alert("请选择文件");
        return;
      }
      // console.log("图片解析为" + file.raw);
      fromData.append("file", file.raw);
      fromData.append("type", "user");
      let { data: res } = await this.$axios.post("/uploadImg",fromData,headerConfig);
      // console.log(res);
      this.imgUrl = res.data.imgurl;
    },

    // 发送from数据到后台
    async onFromUser() {
      this.formAdd.picture = this.imgUrl;
      this.dialogFormVisible = false;
      let {data:res} = await this.$axios.post("/volunteer/add", {
        id: this.formAdd.id,
        position: this.formAdd.position,
        contact: this.formAdd.contact,
        lead: this.formAdd.lead,
        picture: this.formAdd.picture,
      });
      this.getList(this.pageSize, this.pageNum);
      if (res.code == 200) {
        this.$message.success("添加成功");
      } else {
        this.$message.error("添加失败");
      }
    },
    // 发送编辑的数据到后台
    async onUpdateUser() {
      this.dialogFormVisibleInfo = false;
      let {data:res} = await this.$axios.post("/volunteer/update", {
        id: this.fromUpdateUser.id,
        position: this.fromUpdateUser.position,
        contact: this.fromUpdateUser.contact,
        lead: this.fromUpdateUser.lead,
      });
      // console.log(data);
      this.getList(this.pageSize, this.pageNum);
       if( res.code == 200 ){
        this.$message.success('编辑成功');
      } else{
        this.$message.error('添加失败');
      }
    },
    // 获取当前点击编辑的id
    userFrom(e) {
      this.fromUpdateUser.id = e;
      this.dialogFormVisibleInfo = true;
    },
    // 获取点击删除当前的id
    async removeUser(e) {
      let data = await this.$axios.get("/volunteer/delete", {
        params:{
          id: e
        }
      });
      // console.log(data);
      this.getList(this.pageSize, this.pageNum);
       if( data.status == 200 ){
        this.$message.success('删除成功');
      } else{
        this.$message.error('删除失败');
      }
    },

    // 点击之后弹出更多搜索
    addSearch() {
      if(this.Search == true){
        this.Search = false
      }else{
        this.Search = true
      }
    },
    nowTime() {
      //获取当前时间
      let now = new Date();
      let _month =
        10 > now.getMonth() + 1
          ? "0" + (now.getMonth() + 1)
          : now.getMonth() + 1;
      let _day = 10 > now.getDate() ? "0" + now.getDate() : now.getDate();
      let _hour = 10 > now.getHours() ? "0" + now.getHours() : now.getHours();
      let _minute =
        10 > now.getMinutes() ? "0" + now.getMinutes() : now.getMinutes();
      let _second =
        10 > now.getSeconds() ? "0" + now.getSeconds() : now.getSeconds();
      return (
        now.getFullYear() +
        "-" +
        _month +
        "-" +
        _day +
        " " +
        _hour +
        ":" +
        _minute +
        ":" +
        _second
      );
    },
  },
  created() {
    this.getList(this.pageSize, this.pageNum);
  },
};
</script>

<style>
</style>